package com.ubs.lingua.buildtool.cs.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.Random;

/**
 * General-purpose utilities.
 */
public final class CommonUtils {

    private CommonUtils() {
        // should not be instantiated
    }

    private static final Random RANDOM = new Random();

    /**
     * Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified <code>value</code> (exclusive).
     * @param value the bound on the random number to be returned. Must be positive.
     * @return the next pseudorandom, uniformly distributed int value between 0 (inclusive) and <code>value</code>
     *         (exclusive) from this random number generator's sequence
     * @throws IllegalArgumentException if <code>value</code> is not positive
     */
    public static int getRandomNb(final int value) {
        return RANDOM.nextInt(value);
    }

    /**
     * Formats a current Date (a <code>Date</code> object representing this <code>Calendar</code>'s time value) into a date/time string.
     * @param pattern the pattern describing the date and time format
     * @return the formatted date/time string
     */
    public static String getCurrentDate(final String pattern) {
        final SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.ENGLISH);
        return sdf.format(Calendar.getInstance().getTime());
    }

    /**
     * Returns true if the <code>value</code> is null or empty String ("").
     * @param value the value to check
     * @return true if the <code>value</code> is null or empty
     */
    public static boolean isNullOrEmpty(final String value) {
        return (null == value || value.isEmpty());
    }

}
